package com.dgut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dgut.dto.ListFormDto;
import com.dgut.mapper.UserInfoMapper;
import com.dgut.pojo.UserInfo;
import com.dgut.service.UserInfoService;
import com.dgut.utils.MyDateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public Page<UserInfo> getNoFillList(Page<UserInfo> page, ListFormDto listFormDto) {
        String date;
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("1",1);
        if(StringUtils.isNotEmpty(listFormDto.getGender())){
            wrapper.eq("gender",listFormDto.getGender());
        }
        if(StringUtils.isNotEmpty(listFormDto.getDepartment())){
            wrapper.like("department",listFormDto.getDepartment());
        }
        if(StringUtils.isNotEmpty(listFormDto.getClassname())){
            wrapper.like("classname",listFormDto.getClassname());
        }
        if(listFormDto.getCreateTime()!=null){
            date = MyDateUtil.getSimpleDate(listFormDto.getCreateTime());
        }
        else {
            date = MyDateUtil.getSimpleDate(new Date());
        }
        return page.setRecords(userInfoMapper.getNoFillList(page,wrapper,date));
    }

    @Override
    public Page<UserInfo> getUserInfoList(Page<UserInfo> page, ListFormDto listFormDto) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(listFormDto.getName())){
            wrapper.like("name",listFormDto.getName());
        }
        if(StringUtils.isNotEmpty(listFormDto.getGender())){
            wrapper.eq("gender",listFormDto.getGender());
        }
        if(StringUtils.isNotEmpty(listFormDto.getDepartment())){
            wrapper.like("department",listFormDto.getDepartment());
        }
        if(StringUtils.isNotEmpty(listFormDto.getClassname())){
            wrapper.like("classname",listFormDto.getClassname());
        }
        if(StringUtils.isNotEmpty(listFormDto.getSchoolLocation())){
            wrapper.eq("school_location",listFormDto.getSchoolLocation());
        }
        if(StringUtils.isNotEmpty(listFormDto.getOrder())){
            if(listFormDto.getOrder().equals("desc")){
                wrapper.orderByDesc("create_time");
            }
            if(listFormDto.getOrder().equals("asc")){
                wrapper.orderByAsc("create_time");
            }
        }
        return page.setRecords(userInfoMapper.getUserInfoList(page,wrapper));
    }
}
